#include "DisplayObjectContainer.h"

DisplayObjectContainer::DisplayObjectContainer(Uint8 layers)
    : _layers(layers), 
      _objects(new std::set<DisplayObject*>[layers])
{
}

DisplayObjectContainer::~DisplayObjectContainer()
{
    delete _objects;
}

int DisplayObjectContainer::Add(DisplayObject* object, Uint8 layer)
{
    std::pair<std::set<DisplayObject*>::iterator,bool> ret;
    ret = _objects[layer].insert(object);
    
    if (!ret.second)
    {
        printf("DisplayObjectContainer::Add: This element already belongs to the layer. No insertion ocurred..\n");
        return -1;
    }
    
    return 1;
}

int DisplayObjectContainer::Remove(DisplayObject* object, Uint8 layer)
{
    int ret = _objects[layer].erase(object);
    
    if (!ret)
    {
        printf("DisplayObjectContainer::Remove: This element doesnt exists on the layer. Nothing removed.\n");
        return -1;
    }
    
    return 1;
}

void DisplayObjectContainer::Render(SDL_Surface* surface)
{
    forn(i,_layers)
        forall(it,_objects[i])
            (*it)->Render(surface);
}
